var request = require('request')
var fs = require('fs')
var mysql = require('./mydb.js')


// request({url:url,headers:headers},(error, response, body) =>{
// 	if(error) throw error;
// 	fs.appendFile('./1.jpg',body,(err)=>{
// 		if (err) throw err;
// 	})
// });

var download = function (url,name){
	request.head(url, function(err, res, body){
		if(err) throw err;
		request(url).pipe(fs.createWriteStream('./public/image/'+name+'.jpg'));
		console.log(name)
	});
}

mysql.query('select id,name,avatar_url from zhihuperson LIMIT 1000 OFFSET 0',function(err, rows, fields){
	if(err) throw err;
	for(i in rows){
		var name = rows[i].id+rows[i].name;
		var url = rows[i].avatar_url;
		download(url,name);
	}
});